<template>
  <div class="integral-wrap">
    <van-nav-bar
      title="积分商城"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="header">
      <div class="left">
        <div class="span"><span></span><span></span><span></span></div>
        <div class="title">我的积分</div>
        <h1>0</h1>
        <p>获取积分后，积分有效期至 <span>2022年12月31日</span></p>
      </div>
      <div class="right">
        <div class="img"><img src="../assets/image/integral.png" alt="" /></div>
        <div class="span"><span></span><span></span><span></span></div>
      </div>
    </div>
    <div class="integral-content">
      <div class="title">
        <div><div class="span"></div></div>
        <span class="text">积分换券</span>
      </div>
      <div class="card-list" v-for="(list, index) of lists" :key="index">
        <div class="list-item">
          <div class="left">
            <p>
              <b>￥</b><b class="num">{{ list.price }}</b>
            </p>
            <p>满{{ list.range }}元可用</p>
          </div>
          <div class="middle">
            <h4>{{ list.name }}</h4>
            <p>领取后当天有效，有效期15天</p>
            <span>互斥券</span>
          </div>
          <div class="right">
            <p>{{ list.integral }}积分</p>
            <button @click="exchange(index)">立即兑换</button>
          </div>
        </div>
        <div class="list-detail">
          <router-link
            :to="{
              path: '/integraldetail',
              query: { msg: `满${list.range}元减${list.price}元` },
            }"
          >
            查看详情
            <van-icon name="arrow" />
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs } from "@vue/reactivity";
import { Dialog } from "vant";
export default {
  setup() {
    const state = reactive({
      lists: [
        {
          id: 1,
          price: 2,
          range: 10,
          name: "2元立减券",
          integral: 200,
        },
        {
          id: 2,
          price: 4,
          range: 30,
          name: "4元立减券",
          integral: 400,
        },
        {
          id: 3,
          price: 1,
          range: 5,
          name: "1元立减优惠券",
          integral: 100,
        },
        {
          id: 4,
          price: 3,
          range: 20,
          name: "3元立减券",
          integral: 300,
        },
      ],
    });
    const onClickLeft = () => history.back();
    const exchange = (index) => {
      Dialog.confirm({
        title: "提示",
        message: `确定使用${state.lists[index].integral}积分兑换${state.lists[index].name}`,
      })
        .then(() => {
          // on confirm
          Dialog.alert({
            title: "消息提示",
            message: "积分不足",
          }).then(() => {
            // on close
          });
        })
        .catch(() => {
          // on cancel
        });
    };
    return {
      ...toRefs(state),
      onClickLeft,
      exchange,
    };
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.integral-wrap {
  .header {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #f7f7f7;
    box-shadow: 0 0 2px 2px #f7f7f7;
    padding: 0 20px;
    .span {
      height: 4px;
      display: flex;
    }
    span {
      display: inline-block;
      width: 15px;
      height: 4px;
      background-color: #e50055;
      margin-right: 7px;
      border-radius: 2px;
    }
    span:last-child {
      margin-right: 0;
    }
    .left {
      h1 {
        color: #e50055;
        font-size: 38px;
        margin: 0;
      }
      .title {
        margin: 15px 0;
      }
      p {
        span {
          display: inline;
          background-color: #fff;
          color: #ff9f00;
        }
      }
      p:last-child {
        color: #9f9f9f;
        font-size: 13px;
        margin: 0 0 19px 0;
      }
    }

    .right {
      .span {
        margin-left: 10%;
      }
      img {
        margin: 26px 0;
      }
    }
  }
  .integral-content {
    background-color: #fff;
    margin: 2px;
    padding: 10px;
    .title {
      display: flex;
      .span {
        display: inline-block;
        width: 2px;
        height: 16px;
        background-color: #e50055;
        border-radius: 3px;
        margin-right: 8px;
        border: 1px solid #e50055;
      }
      .text {
        font-size: 16px;
        line-height: 28px;
        color: #4d4d4d;
      }
      div {
        padding-top: 3px;
      }
    }
    .card-list {
      border: 1px solid #f7f7f7;
      box-shadow: 0 0 2px 1px #f7f7f7;
      border-radius: 10px;
      box-sizing: border-box;
      font-size: 13px;
      color: #666;
      padding: 0 10px;
      .list-item {
        display: flex;
        justify-content: space-around;
        align-items: center;
        padding: 12px 0;
        .left {
          margin-right: 15px;
          p:first-child {
            color: #f5222d;
            text-align: center;
            font-size: 18px;
            margin: 0;
          }
          .num {
            font-size: 30px;
          }
        }
        .middle {
          margin-right: 15px;
          width: 45%;
          h4 {
            font-size: 15px;
            font-weight: 500;
          }
          p {
            margin: 5px 0;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          span {
            background-color: #feeae6;
            color: #f32d09;
            display: inline-block;
            padding: 2px 5px;
            border-radius: 5px;
          }
        }
        .right {
          p {
            text-align: center;
            color: #f32d09;
            margin-bottom: 3px;
          }
          button {
            border: 0;
            background-color: #e50055;
            color: #fff;
            width: 80px;
            height: 30px;
            border-radius: 20px;
          }
        }
      }
      .list-detail {
        text-align: right;
        border-top: 2px dotted #f7f7f7;
        padding: 8px 0;
      }
    }
  }
}
</style>